package com.journey.org.ui.search

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.journey.org.ui.search.gank.GankSearchViewModel
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.binding.command.BindingConsumer
import com.mvvmhabit.org.bus.Messenger
import com.mvvmhabit.org.bus.event.SingleLiveEvent
import com.mvvmhabit.org.custom.SpinnerItemData

/**
 * @Date: 2020/11/19 10:03
 * @Author:小妖
 * @Description: 搜索
 */
class SearchViewModel(application: Application) : BaseViewModel(application) {

    val isVisibleField = ObservableBoolean(false)
    val isClearVisibleField = ObservableBoolean(false)
    val onInputContentField = ObservableField<String>("")

    override fun onCreate() {
        super.onCreate()
        onLabelItems.add(SpinnerItemData("Android", "android"))
        onLabelItems.add(SpinnerItemData("IOS", "ios"))
        onLabelItems.add(SpinnerItemData("测试", "测试"))
        onLabelItems.add(SpinnerItemData("Android", "android"))
        onLabelItems.add(SpinnerItemData("Android", "android"))
    }

    val onLabelItems = ObservableArrayList<SpinnerItemData>()

    val onLabelSelectedCommand = BindingCommand(object : BindingConsumer<SpinnerItemData> {
        override fun call(t: SpinnerItemData) {
            isVisibleField.set(true)
            Messenger.getDefault()
                .send(t.getValue(), GankSearchViewModel.TOKEN_SEARCH_CONTENT)
        }
    })

    val onFinishCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            finish()
        }
    })

    val onClearSearchCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            onInputContentField.set("")
        }
    })

    val onSearchFocusChangeCommand = BindingCommand(object : BindingConsumer<Boolean> {
        override fun call(t: Boolean) {
            isClearVisibleField.set(t)
        }
    })

    val onSearchCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (onInputContentField.get() == "") {
                isVisibleField.set(false)
                return
            } else {
                isVisibleField.set(true)
                Messenger.getDefault()
                    .send(onInputContentField.get(), GankSearchViewModel.TOKEN_SEARCH_CONTENT)
            }
        }
    })
}